<?php

namespace AdminPanel\MenuBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use AdminPanel\MenuBundle\Entity\Menu;
use AdminPanel\MenuBundle\Form\MenuType;

/**
 * Menu controller.
 *
 */
class MenuController extends Controller
{
    /**
     * Lists all Menu entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('AdminPanelMenuBundle:Menu')->getAllParentMenuByStatus($this->container->getParameter('statusActive'));        
        
        return $this->render('AdminPanelMenuBundle:Menu:index.html.twig', array(
            'entities' => $entities,
        ));
    }
    
    /**
    * Lists all Menu entities.
    *
    */
   public function menuNameListAction()
   {
     $em = $this->getDoctrine()->getManager();

     $entities = $em->getRepository('AdminPanelMenuBundle:Menu')->getAllParentMenuByStatus($this->container->getParameter('statusActive'));
     
     for($i=0;$i<count($entities);$i++)
     {
       $tmpParentId = $entities[$i]['id'];
       $child = $em->getRepository('AdminPanelMenuBundle:Menu')->getChildByParentId($tmpParentId, $this->container->getParameter('statusActive'));
       if($child)
       {
         $childIndex = 0;
         foreach($child AS $childItem)
         {
            $entities[$i]['child'][$childIndex] = $childItem;
            $childIndex++;
         }
       }else{
         $entities[$i]['child'] = null;
       }
     }
     
     return $this->render('AdminPanelMenuBundle:Menu:menuNameList.html.twig', array(
               'entities' => $entities,
           ));
   }

    /**
     * Finds and displays a Menu entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AdminPanelMenuBundle:Menu')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Menu entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('AdminPanelMenuBundle:Menu:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),        ));
    }

    /**
     * Displays a form to create a new Menu entity.
     *
     */
    public function newAction()
    {
        $entity = new Menu();
        $form   = $this->createForm(new MenuType(), $entity);

        return $this->render('AdminPanelMenuBundle:Menu:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Creates a new Menu entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity  = new Menu();
        $form = $this->createForm(new MenuType(), $entity);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('menu_show', array('id' => $entity->getId())));
        }

        return $this->render('AdminPanelMenuBundle:Menu:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing Menu entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AdminPanelMenuBundle:Menu')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Menu entity.');
        }

        $editForm = $this->createForm(new MenuType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('AdminPanelMenuBundle:Menu:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Menu entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AdminPanelMenuBundle:Menu')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Menu entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new MenuType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('menu_edit', array('id' => $id)));
        }

        return $this->render('AdminPanelMenuBundle:Menu:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Menu entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('AdminPanelMenuBundle:Menu')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Menu entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('menu'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
    public function saveMenuListAction()
    {
      // Collect variables
      $arraied = $_POST['list'];
      
      // Sort order
      $i = 1;      
      $msg = true;
      
      foreach($arraied AS $k => $v)
      {
        // Menu update
        $result = $this->getDoctrine()->getManager()->getRepository('AdminPanelMenuBundle:Menu')->updateMenuList($k, $i, $v);        
        if(!$result)
        {
          $msg = 'Хадгалах үйлдэл амжилтгүй боллоо';
        }
        $i++;
      }
      
      // Return result
      if($msg == true)
      {
        echo 'Success';
      }else{
        echo $msg;
      }
      die;
    }
}
